package com.zeng.createmd.restfulmodule.controller;

import com.zeng.createmd.restfulmodule.annotation.Log;
import com.zeng.createmd.restfulmodule.itemCache.ItemCache;
import com.zeng.createmd.restfulmodule.util.*;
import com.zeng.createmd.servicemodule.vo.FileVo;
import com.zeng.createmd.servicemodule.vo.ItemVo;
import com.zeng.createmd.servicemodule.vo.ProjectVo;
import com.zeng.nicecommon.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping
public class ProjectController {

    @Autowired
    FileUtil fileUtil;

    @Autowired
    ItemUtil itemUtil;

    @RequestMapping
    @Log("zhangsan")
    public Object detail(FileVo sysFileVo) {
        return ResponseUtil.success("操作成功").addData("detail", fileUtil.listFile(sysFileVo));
    }

    @RequestMapping
    public Object created(@Validated @NotEmpty(message = "创建目录路径不能为空") String path) {
        File file = new File(path);
        boolean exists = file.exists();
        if (!exists) {
            boolean mkdir = file.mkdir();
            if (mkdir) {
                return ResponseUtil.success("创建目录成功");
            }
        }
        return ResponseUtil.badRequest("此目录已经存在");
    }

    @RequestMapping
    public Object createdProject(@Validated ProjectVo projectVo) {
        Map<String, File> stringFileMap = this.initProjectDir(projectVo.getPath(), projectVo.getGroupId() + "." + projectVo.getArtifactId());
        new ModuleUtil().created(projectVo.getGroupId(), projectVo.getArtifactId(), projectVo.getTablePrefix(), stringFileMap.get("server").getAbsolutePath());
        File file = new File(stringFileMap.get("client"), projectVo.getArtifactId());
        if (!file.exists()) {
            file.mkdirs();
        }
        ZipUtil.unpack(file);
        ZipUtil.unpack("zip/product.zip", new File(file.getParentFile().getParentFile(), "product").getAbsolutePath());
        HashMap<String, Object> info = new HashMap<>();
        info.put("groupId", projectVo.getGroupId());
        info.put("artifactId", projectVo.getArtifactId());
        info.put("tablePrefix", projectVo.getTablePrefix());
        ItemVo project = ItemVo.builder()
                .code(UUID.randomUUID().toString())
                .name(projectVo.getArtifactId())
                .info(info)
                .path(stringFileMap.get("project").getAbsolutePath()).build();
        itemUtil.add(project);
        ItemCache.currentItem = project;
        ProjectUtil projectUtil = new ProjectUtil();
        projectUtil.getData().put("pom", info);
        projectUtil.init();

        return ResponseUtil.success("操作成功");
    }

    @RequestMapping
    public Object listItem() {
        List<ItemVo> itemVos = itemUtil.get();
        return ResponseUtil.success("操作成功").addData("list", itemVos);
    }

    public Map<String, File> initProjectDir(String path, String projectName) {
        File file = new File(path, projectName);
        if (!file.exists()) {
            if (!file.mkdirs()) {
                throw new RuntimeException("项目创建目录失败");
            }
        } else if (!file.isDirectory()) {
            throw new RuntimeException(file.getAbsolutePath() + "不是目录");
        }
        File serverDir = new File(file, "server");
        if (!serverDir.exists()) {
            serverDir.mkdirs();
        }
        File clientDir = new File(file, "client");
        if (!clientDir.exists()) {
            clientDir.mkdirs();
        }
        Map<String, File> returnMap = new HashMap<>();
        returnMap.put("client", clientDir);
        returnMap.put("server", serverDir);
        returnMap.put("project", file);
        return returnMap;
    }

    @RequestMapping
    public Object clear() {
        ProjectUtil projectUtil = new ProjectUtil();
        projectUtil.getData().put("database", null);
        projectUtil.init();
        return ResponseUtil.success("操作成功").addData("data", projectUtil.getData());
    }

}
